package com.hxh.rpc.spring.boot.autoconfiguration;

import com.hxh.rpc.config.RpcConfiguration;
import com.hxh.rpc.provider.RpcProviderBean;
import com.hxh.rpc.registry.ServiceRegistry;
import com.hxh.rpc.registry.zookeeper.ZookeeperRegistry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hxh
 * @date 2022/3/2
 * @since 1.0
 */
@Configuration
@ConditionalOnClass({RpcProviderBean.class})
@EnableConfigurationProperties({RpcConfiguration.class})
public class RpcProviderAutoConfiguration {


    private RpcConfiguration rpcConfiguration;

    public RpcProviderAutoConfiguration(RpcConfiguration rpcConfiguration) {
        this.rpcConfiguration = rpcConfiguration;
    }

    @Bean
    public RpcProviderBean initRpcProvider() throws Exception {
        ServiceRegistry serviceRegistry = new ZookeeperRegistry(rpcConfiguration.getZookeeper());
        return new RpcProviderBean("127.0.0.1:2181", serviceRegistry);
    }
}
